package com.thanhnx.tlg1;

import java.util.Arrays;

import model.MultitouchPlot;
import Util.Point2D;
import Util.Spline2D;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;

import com.androidplot.Plot.BorderStyle;
import com.androidplot.series.XYSeries;
import com.androidplot.ui.AnchorPosition;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XLayoutStyle;
import com.androidplot.xy.XYStepMode;
import com.androidplot.xy.YLayoutStyle;

public class Graph3 extends Activity {
	float max_f = (float) 50.0;
	int max_i = 50;

	private MyApp myApp;

	private MultitouchPlot mySimpleXYPlot;

	private Spline2D spline1;
	private Spline2D spline2;
	private Spline2D spline3;
	private Spline2D spline4;
	private Spline2D spline5;
	private Spline2D spline6;
	private Spline2D spline7;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_graph_3);
		myApp = (MyApp) this.getApplicationContext();

		// initialize our XYPlot reference:
		mySimpleXYPlot = (MultitouchPlot) findViewById(R.id.multitouchPlot);

		// Stype here
		mySimpleXYPlot.setBorderStyle(BorderStyle.NONE, null, null);
		mySimpleXYPlot.setPlotMargins(0, 0, 0, 0);
		mySimpleXYPlot.setPlotPadding(0, 0, 0, 0);
		mySimpleXYPlot.setGridPadding(0, 10, 5, 0);
		mySimpleXYPlot.setBackgroundColor(Color.WHITE);
		mySimpleXYPlot.getGraphWidget().getBackgroundPaint()
				.setColor(Color.WHITE);
		mySimpleXYPlot.getGraphWidget().getGridBackgroundPaint()
				.setColor(Color.WHITE);

		mySimpleXYPlot.getGraphWidget().getDomainLabelPaint()
				.setColor(Color.BLACK);
		mySimpleXYPlot.getGraphWidget().getRangeLabelPaint()
				.setColor(Color.BLACK);

		mySimpleXYPlot.getGraphWidget().getDomainOriginLabelPaint()
				.setColor(Color.BLACK);
		mySimpleXYPlot.getGraphWidget().getDomainOriginLinePaint()
				.setColor(Color.BLACK);
		mySimpleXYPlot.getGraphWidget().getRangeOriginLinePaint()
				.setColor(Color.BLACK);
		// Domain
		mySimpleXYPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, max_f);
		mySimpleXYPlot.setDomainStepValue(1);

		// Range
		// mySimpleXYPlot.setRangeBoundaries(0, 4500, BoundaryMode.FIXED);
		// mySimpleXYPlot.setRangeStepValue(10);

		int size;
		double[] series1Numbers = myApp.getRedBlack_1S();
		size = series1Numbers.length;
		Point2D[] series1Points = new Point2D[size];
		for (int i = 0; i < size; i++) {
			Point2D tempPoint = new Point2D(0, 0);
			tempPoint.x = i + 1;
			tempPoint.y = series1Numbers[i];
			Log.e("abc", " i = " + series1Numbers[i]);
			series1Points[i] = tempPoint;
		}
		double[] series2Numbers = myApp.getRedBlack_2S();
		size = series2Numbers.length;
		Point2D[] series2Points = new Point2D[size];
		for (int i = 0; i < size; i++) {
			series2Points[i] = new Point2D(i + 1, (Double) series2Numbers[i]);
		}
		double[] series3Numbers = myApp.getRedBlack_3S();
		size = series3Numbers.length;
		Point2D[] series3Points = new Point2D[size];
		for (int i = 0; i < size; i++) {
			series3Points[i] = new Point2D(i + 1, (Double) series3Numbers[i]);
		}
		double[] series4Numbers = myApp.getRedBlack_BBL();
		size = series4Numbers.length;
		Point2D[] series4Points = new Point2D[size];
		for (int i = 0; i < size; i++) {
			series4Points[i] = new Point2D(i + 1, (Double) series4Numbers[i]);
		}
		double[] series5Numbers = myApp.getRedBlack_FTL();
		size = series5Numbers.length;
		Point2D[] series5Points = new Point2D[size];
		for (int i = 0; i < size; i++) {
			series5Points[i] = new Point2D(i + 1, (Double) series5Numbers[i]);
		}
		double[] series6Numbers = myApp.getRedBlack_FTL();
		size = series5Numbers.length;
		Point2D[] series6Points = new Point2D[size];
		for (int i = 0; i < size; i++) {
			series6Points[i] = new Point2D(i + 1, (Double) series6Numbers[i]);
		}
		double[] series7Numbers = myApp.getRedBlack_FTL();
		size = series5Numbers.length;
		Point2D[] series7Points = new Point2D[size];
		for (int i = 0; i < size; i++) {
			series7Points[i] = new Point2D(i + 1, (Double) series7Numbers[i]);
		}

		// Apply spline:
		spline1 = new Spline2D(series1Points);
		Number[] series1NumbersPolated = new Number[max_i];
		for (int i = 0; i < max_i; i++) {
			double[] temp = spline1.getPoint(i / max_f);
			series1NumbersPolated[i] = temp[1];
		}
		spline2 = new Spline2D(series2Points);
		Number[] series2NumbersPolated = new Number[max_i];
		for (int i = 0; i < max_i; i++) {
			double[] temp = spline2.getPoint(i / max_f);
			series2NumbersPolated[i] = temp[1];
		}
		spline3 = new Spline2D(series3Points);
		Number[] series3NumbersPolated = new Number[max_i];
		for (int i = 0; i < max_i; i++) {
			double[] temp = spline3.getPoint(i / max_f);
			series3NumbersPolated[i] = temp[1];
		}
		spline4 = new Spline2D(series4Points);
		Number[] series4NumbersPolated = new Number[max_i];
		for (int i = 0; i < max_i; i++) {
			double[] temp = spline4.getPoint(i / max_f);
			series4NumbersPolated[i] = temp[1];
		}
		spline5 = new Spline2D(series5Points);
		Number[] series5NumbersPolated = new Number[max_i];
		for (int i = 0; i < max_i; i++) {
			double[] temp = spline5.getPoint(i / max_f);
			series5NumbersPolated[i] = temp[1];
		}
		spline6 = new Spline2D(series6Points);
		Number[] series6NumbersPolated = new Number[max_i];
		for (int i = 0; i < max_i; i++) {
			double[] temp = spline5.getPoint(i / max_f);
			series6NumbersPolated[i] = temp[1];
		}
		spline7 = new Spline2D(series7Points);
		Number[] series7NumbersPolated = new Number[max_i];
		for (int i = 0; i < max_i; i++) {
			double[] temp = spline5.getPoint(i / max_f);
			series7NumbersPolated[i] = temp[1];
		}

		// Turn the above arrays into XYSeries':
		XYSeries series1 = new SimpleXYSeries(
				Arrays.asList(series1NumbersPolated),
				SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "1S");
		XYSeries series2 = new SimpleXYSeries(
				Arrays.asList(series2NumbersPolated),
				SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "11S");
		XYSeries series3 = new SimpleXYSeries(
				Arrays.asList(series3NumbersPolated),
				SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "3S");
		XYSeries series4 = new SimpleXYSeries(
				Arrays.asList(series4NumbersPolated),
				SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "BBL");
		XYSeries series5 = new SimpleXYSeries(
				Arrays.asList(series5NumbersPolated),
				SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "FTL");
		XYSeries series6 = new SimpleXYSeries(
				Arrays.asList(series6NumbersPolated),
				SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "ARD");
		XYSeries series7 = new SimpleXYSeries(
				Arrays.asList(series7NumbersPolated),
				SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "ABK");

		// Create a formatter to use for drawing a series using
		// LineAndPointRenderer:
		LineAndPointFormatter series1Format = new LineAndPointFormatter(
				Color.rgb(0, 200, 0), // line color
				null, // point color
				null); // fill color (none)

		// add a new series' to the xyplot:
		mySimpleXYPlot.addSeries(series1, series1Format);

		// same as above:
		mySimpleXYPlot.addSeries(series2,
				new LineAndPointFormatter(Color.rgb(0, 0, 200), null, null));
		mySimpleXYPlot.addSeries(series3,
				new LineAndPointFormatter(Color.rgb(200, 0, 0), null, null));
		mySimpleXYPlot.addSeries(series4,
				new LineAndPointFormatter(Color.rgb(200, 0, 200), null, null));
		mySimpleXYPlot.addSeries(series5,
				new LineAndPointFormatter(Color.rgb(200, 200, 0), null, null));
		mySimpleXYPlot.addSeries(series6,
				new LineAndPointFormatter(Color.rgb(0, 200, 0), null, null));
		mySimpleXYPlot.addSeries(series7,
				new LineAndPointFormatter(Color.rgb(0, 200, 200), null, null));

		// reduce the number of range labels
		mySimpleXYPlot.setTicksPerRangeLabel(3);
		// by default, AndroidPlot displays developer guides to aid in laying
		// out your plot.
		// To get rid of them call disableAllMarkup():
		mySimpleXYPlot.disableAllMarkup();
	}
}